package com.gitee.qdbp.general.common.web.controller.files;

import java.io.IOException;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.able.result.ResponseMessage;
import com.gitee.qdbp.able.result.ResultCode;
import com.gitee.qdbp.base.annotation.OperateRecord;
import com.gitee.qdbp.base.utils.WebFileHandler;
import com.gitee.qdbp.general.common.api.files.model.FileAuthorization;
import com.gitee.qdbp.general.common.api.files.model.FolderName;
import com.gitee.qdbp.general.common.api.files.service.IWebFileService;
import com.gitee.qdbp.tools.utils.VerifyTools;

/**
 * 文件上传下载控制器
 *
 * @author zhaohuihua
 * @version 150325
 */
@Controller
@RequestMapping("/actions/webfile")
public class WebFileController {

    @Autowired
    private IWebFileService webFileService;

    private WebFileHandler webFileHandler;

    @PostConstruct
    protected void init() {
        this.webFileHandler = new WebFileHandler(webFileService);
    }

    /**
     * 文件上传<br>
     * 文件扩展名必须在allow.upload.suffix配置中<br>
     * 
     * @param file 上传的文件内容
     * @param folder 根文件夹, 可为空(默认为files)
     * @param filename 文件名, 用来获取文件扩展名
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    @OperateRecord("文件上传")
    public ResponseMessage upload(@RequestParam("file") CommonsMultipartFile file, String folder, String filename,
            String extra) {

        try {
            String url = webFileHandler.upload(file, folder, filename, extra);
            return new ResponseMessage(url);
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }

    }

    /**
     * 下载文件<br>
     * 文件扩展名必须在allow.download.suffix配置中<br>
     * 文件必须位于文件服务配置的文件夹或allow.download.folder配置中<br>
     * 如果: download.location.xxx = alias /home/tomcat/runtimelogs/<br>
     * 那么: path=xxx/web-user-error.log = /home/tomcat/runtimelogs/web/user-error.log文件<br>
     * 如果: download.location.xxx =  root /home/tomcat/runtimelogs/<br>
     * 那么: path=xxx/web/user-error.log = /home/tomcat/runtimelogs/xxx/web/user-error.log文件<br>
     * 完整配置<br>
     * download.location.xxx = alias /home/tomcat/runtimelogs/<br>
     * allow.download.suffix.location.xxx = .log|.err|.txt<br>
     * 
     * @param path 文件的保存路径
     * @param filename 下载时浏览器提示的文件名(可为空,从保存路径中截取)
     * @throws IOException
     * @throws ServiceException
     */
    @RequestMapping("download")
    @OperateRecord("文件下载")
    public void download(HttpServletRequest request, HttpServletResponse response, String path, String filename)
            throws IOException, ServiceException {

        if (VerifyTools.isBlank(path)) {
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        webFileHandler.download(request, response, path, filename);
    }

    @ResponseBody
    @RequestMapping("authorize")
    @OperateRecord(enable = false)
    public ResponseMessage authorize(FolderName group) {
        try {
            FileAuthorization authorization = webFileService.authorize(group);
            return new ResponseMessage(authorization);
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }
}
